#!/usr/bin/env python
# encoding: utf-8
"""
board.py

Created by Morgan Quirk on 2007-07-04.
"""

class Board:
    def __init__(self, pos, size):
        self.x, self.y=pos
        self.w, self.h=size
        self.grid=[]
        self.colors=[]
        for x in range(self.w):
            self.grid.append([])
            self.colors.append([])            
            for y in range(self.h):
                self.grid[x].append(0)
                self.colors[x].append(None)
                
    def add_piece(self, piece):
        loss=0
        for (x,y) in piece.rotations[piece.rotation]:
            if y+piece.y<0:
                loss=1
            else:
                self.grid[x+piece.x][y+piece.y]=1
                self.colors[x+piece.x][y+piece.y]=[max(0,v-100) for v in piece.color]
            
        if loss==1:
            return 1
            
    
    def get_filled(self):
        filled=[]
        for x in range(self.w):
            for y in range(self.h):
                if self.grid[x][y]:
                    filled.append((x,y))
        return filled
        
    def drop(self, y):
        for y in range(y,0,-1):
            for x in range(self.w):
                self.grid[x][y]=self.grid[x][y-1]
                self.colors[x][y]=self.colors[x][y-1]                
        for x in range(self.w):
            self.grid[x][0]=0
            self.colors[x][0]=None